package com.util;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Style;
/**
 * 自定义按钮
 * @author Administrator
 *
 */
public class Button { 
	private Bitmap bitmapNormal;
	private Bitmap bitmapClicked;
	public Rect rect_btn;
	public boolean is_clicked=false;
	private Paint btn_paint = new Paint();
	protected int x,y;
	
	public Button(Bitmap _bitmapNormal,Bitmap _bitmapClicked, int _x, int _y) {
		x = _x;
		y = _y;
		rect_btn = new Rect(_x,_y,_x+_bitmapNormal.getWidth(),_y+_bitmapNormal.getHeight());
		bitmapNormal = _bitmapNormal;
		bitmapClicked = _bitmapClicked;
		btn_paint.setStyle(Style.STROKE);
		btn_paint.setColor(0xffff0000);
		
	}
	/**
	 * 是否被按下
	 * @param _touchx
	 * @param _touchy
	 * @return
	 */
	public boolean isClicked(int _touchx,int _touchy){
		if(rect_btn.contains(_touchx, _touchy)){
				is_clicked = true;	
			return true;
		}
		return false;
	}
	/**
	 * 是否被按下
	 * @param _touchx
	 * @param _touchy
	 * @return
	 */
	public boolean isClicked1(int _touchx,int _touchy){
		if(rect_btn.contains(_touchx, _touchy)){
			if(!is_clicked){
				is_clicked = true;
			}else if(is_clicked){
				is_clicked = false;
			}
			return true;
		}
		return false;
	}
	public boolean isClicked2(int _touchx,int _touchy){
		if(rect_btn.contains(_touchx, _touchy)){
			return true;
		}
		return false;
	}
	public void setClickOff(){
		is_clicked = false;
	}
	public void setClickOn(){
		is_clicked = true;
	}
	/**
	 * 画按钮
	 * @param canvas
	 */
	public void draw(Canvas canvas){
//		canvas.drawRect(rect_btn,btn_paint);
		if(is_clicked){
			canvas.drawBitmap(bitmapClicked, x,y, btn_paint);
		}else{
			canvas.drawBitmap(bitmapNormal, x,y, btn_paint);
		}
	}
	public void setPosition(int _x,int _y){
		x = _x;
		y = _y;
		rect_btn.left = _x;
		rect_btn.top = _y;
		rect_btn.right = _x+bitmapNormal.getWidth();
		rect_btn.bottom = _y+bitmapNormal.getHeight();
	}
	/**
	 * 释放
	 */
	public void recycle(){
		if(bitmapClicked!=null){
			bitmapClicked.recycle();
		}
		if(bitmapNormal!=null){
			bitmapNormal.recycle();
		}
	}
	public int getX() {
		return x;
	}
	public int getY() {
		return y;
	}
}
